package org.flint92.springcloud.hystrix.command;

import com.google.common.collect.ImmutableList;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import org.flint92.user.domain.User;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author flint92
 */
public class UserClientHystrixCommand extends HystrixCommand<List<User>> {

    private final String providerServiceName;

    private final RestTemplate restTemplate;

    public UserClientHystrixCommand(String providerServiceName, RestTemplate restTemplate) {
        super(HystrixCommandGroupKey.Factory.asKey("User-Client"), 500);
        this.providerServiceName = providerServiceName;
        this.restTemplate = restTemplate;
    }


    @Override
    @SuppressWarnings("unchecked")
    protected List<User> run() throws Exception {
        // url
        String url = "http://" + providerServiceName + "/user-provider/list";

        return restTemplate.getForObject(url, List.class);
    }

    @Override
    protected List<User> getFallback() {
        return ImmutableList.of();
    }
}
